package com.sy.profile.sys.user.controller;

import com.sy.core.base.common.Page;
import com.sy.core.base.controller.BaseController;
import com.sy.core.base.service.BaseServiceImpl;
import com.sy.core.exception.BusinessException;
import com.sy.profile.sys.user.model.User;
import com.sy.profile.sys.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户控制层类
 *
 * @author lqw
 * @version 1.0
 * @Date 2011年12月20日 13:51:13
 */

@Controller
@RequestMapping(value = "user")
public class UserController extends BaseController<User> {

    @Autowired
    private UserService userService;


    /**
     * 获取列表数据(分页)
     *
     * @param request
     * @param response
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "list")
    @ResponseBody
    public Page<?> list(Page<?> page, HttpServletRequest request,
                        HttpServletResponse response) throws Exception {
        try {
            page = userService.getPage(page, getSearchMap(request));
        } catch (Exception e) {
            throw new Exception(e);
        }
        return page;
    }


    /**
     * 跳转到新增页面
     *
     * @param request
     * @param response
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "toAdd")
    public ModelAndView toAdd(HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        getSearchMap(request);
        return view("add");
    }


    /**
     * 新增账号
     *
     * @param user
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "add")
    public ModelAndView add(User user, HttpServletRequest request,
                            HttpServletResponse response) throws Exception {
        userService.add(user);
        return view("add");
    }
}